# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  # protect_from_forgery # :secret => '450a532f34227e7b55f62f6abc2ea79a'
  
  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  # filter_parameter_logging :password
  layout "facebook"
  
  filter_parameter_logging :password
  ensure_authenticated_to_facebook [:except=>["buy_point","from_chat","adparlor"]]
  helper_method :facebook_session
  before_filter :current_user,:except=>[:buy_point,:from_chat,:adparlor]  
  before_filter :set_app_name,:except=>[:buy_point,:from_chat,:adparlor]  
  before_filter :update_friends,:except=>[:buy_point,:from_chat,:adparlor]  

  def current_user
    @current_user ||= User.for(facebook_session)
  end

  def initialize
    @odd = 0
  end 
  def set_app_name
    @app_name = Facebooker.facebooker_config["canvas_page_name"]
    @call_back_url = Facebooker.facebooker_config["callback_url"].gsub("http://","")
  end

  def update_friends
    if current_user.all_friends.nil? and params[:fb_sig_friends] 
       current_user.all_friends = params[:fb_sig_friends]
       current_user.save
    end
    if current_user.timezone.nil? 
       current_user.timezone = facebook_session.user.timezone
       current_user.save
    end
  end

  def deliver_notify
    logger.error "==========================="
    logger.error NotificationPublisher.deliver_challenge(current_user.sent_challenges.first) if current_user.sent_challenges.size > 0
    logger.error "==========================="
  end

  helper_method :current_user

end
